package kd.bos.kdec.pincar;

import java.text.NumberFormat;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

/**
 * 拼车订单评价
 * @author zhangyizhe
 *
 */
public class PinCarForMobEvaluate extends AbstractFormPlugin {

	private String[] buttons = new String[] { "submit", "return", "star1", "star2", "star3", "star4", "star5", "star1y",
			"star2y", "star3y", "star4y" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		IFormView view = this.getView();
		IDataModel model = this.getModel();
		if (StringUtils.equals("submit", key)) {// 提交逻辑
			view.setVisible(false, "flexping");
			view.setVisible(false, "flexfw");
			view.setVisible(true, "flexfin");
			view.setVisible(true, "return");
		} else if (StringUtils.equals("return", key)) {// 首页跳转
			Map<String, Object> formId = new HashMap<>();
			formId.put("formId", "kdec_pincar");
			FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
			showParameter.getOpenStyle().setShowType(ShowType.Floating);
			view.showForm(showParameter);
		} else if (StringUtils.equals("star1", key)) {// 一星
			view.setVisible(false, "star1");
			view.setVisible(true, "star1y");
			model.setValue("starnum", 1);
		} else if (StringUtils.equals("star2", key)) {
			view.setVisible(false, "star1");
			view.setVisible(true, "star1y");
			view.setVisible(false, "star2");
			view.setVisible(true, "star2y");
			model.setValue("starnum", 2);
		} else if (StringUtils.equals("star3", key)) {
			view.setVisible(false, "star1");
			view.setVisible(true, "star1y");
			view.setVisible(false, "star2");
			view.setVisible(true, "star2y");
			view.setVisible(false, "star3");
			view.setVisible(true, "star3y");
			model.setValue("starnum", 3);
		} else if (StringUtils.equals("star4", key)) {
			view.setVisible(false, "star1");
			view.setVisible(true, "star1y");
			view.setVisible(false, "star2");
			view.setVisible(true, "star2y");
			view.setVisible(false, "star3");
			view.setVisible(true, "star3y");
			view.setVisible(false, "star4");
			view.setVisible(true, "star4y");
			model.setValue("starnum", 4);
		} else if (StringUtils.equals("star5", key)) {
			view.setVisible(false, "star1");
			view.setVisible(true, "star1y");
			view.setVisible(false, "star2");
			view.setVisible(true, "star2y");
			view.setVisible(false, "star3");
			view.setVisible(true, "star3y");
			view.setVisible(false, "star4");
			view.setVisible(true, "star4y");
			view.setVisible(false, "star5");
			view.setVisible(true, "star5y");
			model.setValue("starnum", 5);
		} else if (StringUtils.equals("star4y", key)) {
			view.setVisible(false, "star5y");
			view.setVisible(true, "star5");
			model.setValue("starnum", 4);
		} else if (StringUtils.equals("star3y", key)) {
			view.setVisible(false, "star5y");
			view.setVisible(true, "star5");
			view.setVisible(false, "star4y");
			view.setVisible(true, "star4");
			model.setValue("starnum", 3);
		} else if (StringUtils.equals("star2y", key)) {
			view.setVisible(false, "star5y");
			view.setVisible(true, "star5");
			view.setVisible(false, "star4y");
			view.setVisible(true, "star4");
			view.setVisible(false, "star3y");
			view.setVisible(true, "star3");
			model.setValue("starnum", 2);
		} else if (StringUtils.equals("star1y", key)) {
			view.setVisible(false, "star5y");
			view.setVisible(true, "star5");
			view.setVisible(false, "star4y");
			view.setVisible(true, "star4");
			view.setVisible(false, "star3y");
			view.setVisible(true, "star3");
			view.setVisible(false, "star2y");
			view.setVisible(true, "star2");
			model.setValue("starnum", 1);
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		IFormView view = this.getView();
		IDataModel model = this.getModel();
		FormShowParameter parameter = this.getView().getFormShowParameter();
		if (null != parameter.getCustomParam("driver")) {
			model.setValue("driver", parameter.getCustomParam("driver"));
		}
		// 订单评价页面加载时查询有多少笔完成订单
		long userId = Long.parseLong(RequestContext.get().getUserId());
		QFilter filter = new QFilter("creator", QCP.equals, userId);
		filter.and(new QFilter("orderstatus", QCP.equals, 2));
		DynamicObjectCollection collection = QueryServiceHelper.query("kdec_pincarorder", "id", filter.toArray());
		int orderNum = collection.size();// 完成订单数
		Label labelor = view.getControl("ordertext");
		labelor.setText(String.valueOf(orderNum));
		String fstar = this.fiveStarMathPer(userId);
		Label label = view.getControl("fstar");
		label.setText(fstar);
	}

	private String fiveStarMathPer(long userId) {
		// 查五星的订单
		QFilter filterEva = new QFilter("driver", QCP.equals, userId);
		DynamicObjectCollection collectionEva = QueryServiceHelper.query("kdec_pincar_evaluate", "id,starnum",
				filterEva.toArray());
		int evaNum = collectionEva.size();// 总评价数
		if (evaNum == 0) {
			return 0+"%";
		}
		int eva5s = 0;
		for (DynamicObject dynamicObject : collectionEva) {
			Long starNum = dynamicObject.getLong("starnum");
			if (starNum.equals(Long.valueOf(5))) {
				eva5s = eva5s + 1;
			}
		}
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(1);
		String result = numberFormat.format((float) eva5s / (float) evaNum * 100);
		String fstar =  result + "%";
		return fstar;
	}

	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
	}

	@Override
	public void beforeClick(BeforeClickEvent evt) {
		super.beforeClick(evt);
		IDataModel model = this.getModel();
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		IFormView view = this.getView();
		if (StringUtils.equals("submit", key)) {// 提交逻辑
			if (null == model.getValue("radiogroupfield")
					&& null == model.getValue("radiogroupfield1")
					&& null == model.getValue("radiogroupfield2")
					&& null == model.getValue("radiogroupfield3")
					&& StringUtils.equals("", model.getValue("evatext").toString())) {
				view.showMessage("请至少填写一条评价");
				evt.setCancel(true);
				return;
			}else if (Integer.valueOf(model.getValue("starnum").toString())==0) {
				view.showMessage("请评价星级");
				evt.setCancel(true);
				return;
			}
		}
	}
}
